package football_behaviour

import (
	"github.com/shopspring/decimal"
	"longmen/server/internal/services/nami/utils"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"strconv"
)

type (
	OddsVars    []interface{}
	OddsCollect map[string][]OddsVars
	// OddsHistoryResult 公司ID > 赔率集合
	OddsHistoryResult      map[string]OddsCollect
	OddsHistoryApiResponse struct {
		Code    int               `json:"code"`
		Results OddsHistoryResult `json:"results"`
	}
)

func (odds OddsHistoryResult) GetByOdds(matchId int) []models_sports.FootballOdds {
	records := make([]models_sports.FootballOdds, 0)
	for cid, collect := range odds {
		companyId, _ := strconv.Atoi(cid)
		for exType, odds := range collect {
			for _, odd := range odds {
				odds2 := decimal.NewFromFloat(utils.GetFloat64(odd[3]))
				// 正变负，负变正，0不变
				if exType == "asia" {
					// 正变负，负变正，0不变
					if odds2.LessThan(decimal.Zero) {
						odds2 = odds2.Abs()
					} else if odds2.GreaterThan(decimal.Zero) {
						odds2 = odds2.Neg()
					}
				}
				records = append(records, models_sports.FootballOdds{
					CompanyId:        companyId,
					ExponentialType:  exType,
					ExponentialScore: odd[7].(string),
					MatchId:          matchId,
					MatchStatus:      utils.GetInt(odd[5]),
					OddsStatus:       utils.GetInt(odd[6]),
					Odds1:            decimal.NewFromFloat(utils.GetFloat64(odd[2])),
					Odds2:            odds2,
					Odds3:            decimal.NewFromFloat(utils.GetFloat64(odd[4])),
					ChangeTime:       utils.GetInt64(odd[0]),
					CompetitionTime:  odd[1].(string),
				})
			}
		}
	}
	return records
}
